﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.Common;
using DataBase.Common;
using DataBase;

using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Diagnostics;
using Utility.IO;
using Utility.Tool;
using Utility.Data;
using Utility.Encrypt;
using Utility.Config;

namespace DataBase.Core
{
    /// <summary>
    /// 数据库通用操作层(微软企业库5.1)
    /// 版本：2.0
    /// </summary>
    internal class SqlServerHelper : DbBase
    {
        public override string Pre
        {
            get
            {
                return "@";
            }
        }

        #region 构造函数
        public SqlServerHelper(string conn)
            : base(conn)
        { }
        public SqlServerHelper()
            : base(DBConfig.ConnString)
        { }
        #endregion

        #region 数据分页

        /// <summary>
        /// 数据分页
        /// </summary>
        /// <param name="sql">sql语句：当作数据表使用</param>
        /// <param name="ht">参数</param>
        /// <param name="sortBy">排序 如：id desc </param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">总记录数</param>
        /// <returns>DataTable</returns>
        public override DataTable GetPageList(string sql, Hashtable ht, string sortBy, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                int num = (pageIndex - 1) * pageSize;
                int num1 = (pageIndex) * pageSize;
                sb.Append("Select * From (Select ROW_NUMBER() Over (Order By " + sortBy);
                sb.Append(") As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
                count = Convert.ToInt32(this.GetObjectValue("Select Count(1) From (" + sql + ") As t", ht));
                return this.GetDataTableBySQL(sb.ToString(), ht);
            }
            catch (Exception e)
            {
                Logger.WriteLog("-----------数据分页（SqlServer）-----------\r\n" + sb.ToString() + "\r\n" + e.Message + "\r\n");
                return null; ;
            }
        }
        public override DataTable GetPageList(string tableName, string primaryKey, string showFields, int pageIndex, int pageSize, string whereStr, string sortBy, ref int count)
        {
            StringBuilder sb = new StringBuilder();
            count = 0;
            try
            {
                int num = (pageIndex - 1) * pageSize;
                int num1 = (pageIndex) * pageSize;
                sb.Append("Select * From (Select ROW_NUMBER() Over (Order By " + sortBy);
                sb.Append(") As rowNum, * From tableName ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
                count = Convert.ToInt32(this.GetObjectValue("Select Count(1) From tableName Where " + whereStr));
                return this.GetDataTableBySQL(sb.ToString());
            }
            catch (Exception e)
            {
                Logger.WriteLog("-----------数据分页（SqlServer）-----------\r\n" + sb.ToString() + "\r\n" + e.Message + "\r\n");
                return null; ;
            }
        }
        #endregion

        #region BulkCopy 批量导入
        /// <summary>
        /// 利用Net OracleBulkCopy 批量导入数据库,速度超快
        /// </summary>
        /// <param name="dt">源内存数据表</param>
        /// <returns></returns>
        public override bool BulkCopyImport(DataTable dt)
        {
            IDbHelperExpand copy = new IDbHelperExpand();
            return copy.MsSqlBulkCopyData(dt, connectionString);
        }
        #endregion
    }
}
